import { getObjectType, matchArray } from '../../../core/utils/utils';
import { EquipTypeDefine } from '../../defines/equip_define';
import { LimbsTypeDefine } from '../../defines/people_limbs';
import { BasicGoods } from './basic_goods';

// 装备物品，可以穿着卸下分解
export class EquipGoods extends BasicGoods{
	/**
	 * 装备的类型，在同一肢体上不能装备相同类型的装备
	 */
	getEquipZIndex(){
    return EquipTypeDefine.equip.key;
	}

  /**
   * 是否可以与相同类型装备共存
   * @returns 
   */
  getZIndexIsCoexist(){
    return false;
  }

  /**
   * 装备携带的技能
   * @returns 
   */
  getSkills(){
    return [];
  }

  /**
   * 装备这件装备完成时触发
   */
  onEquiped(){

  }

  /**
   * 卸下这件装备中，返回值true将正常卸下，如果返回文本将会卸下失败
   */
	onUnEquip(){
		return true;
	}

  /**
   * 卸下这件装备完成时触发
   */
  onUnEquiped(){

  }

  // 只有在背包中有效
  getControlEquipCondition(){
    return this.backpackId != null && this.getIsEquip();
  }

	getControls() {
    let controls = super.getControls();

    if(this.peopleLimb){
      controls = [{ key: 'UnEquip', text: '卸下' }];
    } else {
      controls.splice(1, 0, 
        { key: 'Equip', text: '装备' }
      )
    }

    return controls;
	}

  getControlEquip(){
    let needLimbs = this.getIsEquip();
    
    let { optionSelected } = this.root.gameState.dialogs.showOptionChooser('装备给谁', {
      active: false,
      options: this.root.playerData.troops.filter(item => {
        if(!this.getIsRace() || item.data.race == this.getIsRace()) return true;
      }).map(item => ({
        value: item.data._id,
        text: item.getName()
      }))
    });

    optionSelected.add(value => {
      let people = this.root.findPlayerPeople(value);
      // 查询符合要求的肢体
      let limbs = people.getLimbs(needLimbs);

      let { optionSelected } = this.root.gameState.dialogs.showOptionChooser('选择肢体', {
        active: false,
        options: limbs.map(item => ({
          value: item.text,
          text: item.text || LimbsTypeDefine[item.key].text
        }))
      })

      optionSelected.add(value => {
        let limb = limbs.find(item => item.text == value);
        let needAttrError = this.getPeopleMatchNeedAttr(people);

        if(needAttrError){
          this.root.gameState.dialogs.showWarning('无法装备', needAttrError.join('<br />'));
          return;
        }

        // if(limb.status == 0){
        //   this.root.gameState.dialogs.showWarning('肢体已残疾', '你的这个肢体已经残疾无法使用');
        //   return;
        // }

        let existsZIndex = limb.equips.find(item => {
          return item.getEquipZIndex() == this.getEquipZIndex()
        })
        if(existsZIndex && !this.getZIndexIsCoexist()){
          this.root.gameState.dialogs.showWarning('无法装备', '你的这个肢体已经装备了相同类型的装备。');
          return;
        }

        if(limb.equipNum <= limb.equips.length){
          this.root.gameState.dialogs.showWarning('肢体装备已满', '你的这个肢体已经装满了装备，请卸下装备再进行操作。');
          return;
        }

        let good = this;

        if(!this.getIsStack() && this.attr.data.num > 1){
          good = this.root.worldMap.getGood(this.key, {
            ...(this.attr.data),
            num: 1
          });
          this.root.backdrop.consume(this.backpackId, 1);
        } else {
          this.root.backdrop.remove(this.backpackId);
        }
        good.peopleLimb = limb;
        good.equipsIndex = limb.equips.length;
        limb.equips.push(good);

        good.runComponent('onEquiped');
        this.root.hud.parts.playerPage.drawEquip();

        this.root.message('alert', `装备<${this.getTitle()}>`);
        this.root.savePlayerData();
      })
    })
  }

  getControlUnEquip(){
    let flag = this.runComponent('onUnEquip');
    if(getObjectType(flag) == 'String') {
      this.root.gameState.dialogs.showWarning('卸下失败', flag);
      return;
    }

    let isPut = this.root.backdrop.getGoodAndPut(this, null, false);
    if(!isPut) return;

    this.peopleLimb.equips = this.peopleLimb.equips.filter((item, i) => {
      if(i != this.equipsIndex) return true;
    })
    this.peopleLimb = null;

    this.root.hud.parts.playerPage.drawEquip();

    this.runComponent('onUnEquiped');
    this.root.message('alert', `卸下<${this.getTitle()}>`);
    this.root.savePlayerData();
  }

  /**
   * ----------------- 内部方法 -----------------
   */
}